package com.hongye.consult.service.impl;

import com.hongye.consult.dao.BrokerDao;
import com.hongye.consult.entity.HongyeAuditEntity;
import com.hongye.consult.entity.HouseEntity;
import com.hongye.consult.service.BrokerService;
import com.hongye.consult.vo.BrokerParticularsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author fyd
 * @email 2104371759@qq.com
 * @date 2022/4/27
 * @time 8:38
 */
@Service
public class BrokerServiceImpl implements BrokerService {

    @Autowired(required = false)
    private BrokerDao brokerDao;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 经纪人详情
     * @param id 经纪人编号
     * @return
     */
    @Override
    public BrokerParticularsVo getBrokerParticulars(Integer id){
        BrokerParticularsVo particularsVo = new BrokerParticularsVo();
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            BrokerParticularsVo userinfo = brokerDao.getUserinfo(id);
            particularsVo.setId(userinfo.getId());
            particularsVo.setName(userinfo.getName());
            particularsVo.setUserName(userinfo.getUserName());
            particularsVo.setImgUrl(userinfo.getImgUrl());
            particularsVo.setCompany(userinfo.getCompany());
            particularsVo.setUserPhone(userinfo.getUserPhone());
            particularsVo.setServiceIntroduction(userinfo.getServiceIntroduction());
        }, threadPoolExecutor);

        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            List<HouseEntity> houseEntities = brokerDao.getHouseInfo(id);
            particularsVo.setHouse(houseEntities);
        }, threadPoolExecutor);

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(voidCompletableFuture1, voidCompletableFuture2);

        return particularsVo;
    }

    /**
     * 我想租个房
     * @param entity
     * @return
     */
    @Override
    public Integer addAuditByRenting(HongyeAuditEntity entity) {
        //查找类型
        String typeName = brokerDao.selectTypeById(entity.getIssueTypeId());
        entity.setIssueTypeName(typeName);
        //添加
        int i = brokerDao.addAuditByRenting(entity);
        return i;
    }


}
